package com.ruoyi.zxjd.work.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zxjd.work.domain.WorkTemplate;
import com.ruoyi.zxjd.work.service.IWorkTemplateService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工作模板Controller
 * 
 * @author lcc
 * @date 2021-01-21
 */
@Controller
@RequestMapping("/work/template")
public class WorkTemplateController extends BaseController
{
    private String prefix = "work/template";

    @Autowired
    private IWorkTemplateService workTemplateService;

    @RequiresPermissions("work:template:view")
    @GetMapping()
    public String template()
    {
        return prefix + "/template";
    }

    /**
     * 查询工作模板列表
     */
    @RequiresPermissions("work:template:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WorkTemplate workTemplate)
    {
        startPage();
        List<WorkTemplate> list = workTemplateService.selectWorkTemplateList(workTemplate);
        return getDataTable(list);
    }

    /**
     * 导出工作模板列表
     */
    @RequiresPermissions("work:template:export")
    @Log(title = "工作模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WorkTemplate workTemplate)
    {
        List<WorkTemplate> list = workTemplateService.selectWorkTemplateList(workTemplate);
        ExcelUtil<WorkTemplate> util = new ExcelUtil<WorkTemplate>(WorkTemplate.class);
        return util.exportExcel(list, "template");
    }

    /**
     * 新增工作模板
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存工作模板
     */
    @RequiresPermissions("work:template:add")
    @Log(title = "工作模板", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WorkTemplate workTemplate)
    {
        return toAjax(workTemplateService.insertWorkTemplate(workTemplate));
    }

    /**
     * 修改工作模板
     */
    @GetMapping("/edit/{templateId}")
    public String edit(@PathVariable("templateId") Long templateId, ModelMap mmap)
    {
        WorkTemplate workTemplate = workTemplateService.selectWorkTemplateById(templateId);
        mmap.put("workTemplate", workTemplate);
        return prefix + "/edit";
    }

    /**
     * 修改保存工作模板
     */
    @RequiresPermissions("work:template:edit")
    @Log(title = "工作模板", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WorkTemplate workTemplate)
    {
        return toAjax(workTemplateService.updateWorkTemplate(workTemplate));
    }

    /**
     * 删除工作模板
     */
    @RequiresPermissions("work:template:remove")
    @Log(title = "工作模板", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(workTemplateService.deleteWorkTemplateByIds(ids));
    }

    /**
     * 工作模板启用停用
     */
    @Log(title = "工作模板", businessType = BusinessType.UPDATE)
    @RequiresPermissions("work:template:enable-disable")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(WorkTemplate workTemplate)
    {
        return toAjax(workTemplateService.changeStatus(workTemplate));
    }

    /**
     * 跳转定义模板
     */
    @RequiresPermissions("work:template:define")
    @GetMapping("/define/{templateId}")
    public String define(@PathVariable("templateId") Long templateId, ModelMap mmap)
    {
        WorkTemplate workTemplate = workTemplateService.selectWorkTemplateById(templateId);
        mmap.put("workTemplate", workTemplate);
        return prefix + "/define";
    }

    /**
     * 获取整个工作簿
     */
    @PostMapping("/loadTemplateContent")
    @ResponseBody
    public String loadTemplateContent(Long gridKey)
    {
        WorkTemplate workTemplate = workTemplateService.selectWorkTemplateById(gridKey);
        return workTemplate.getTemplateContent();
    }
}
